﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace Shared
{
    public class CheckExistsForms
    {
        private static readonly Dictionary<Type, Form> singleInstanceForms = new Dictionary<Type, Form>();

        public static Form MdiParent { get; set; }

        public static Form ActivateForm<T>() where T : Form, new()
        {
            if (!singleInstanceForms.ContainsKey(typeof(T)))
            {
                T newForm = new T();
                newForm.MdiParent = MdiParent;
                newForm.FormClosed += new FormClosedEventHandler(delegate(object sender, FormClosedEventArgs e)
                {
                    singleInstanceForms.Remove(sender.GetType());
                });
                singleInstanceForms.Add(typeof(T), newForm);
            }
            Form formToActivate = singleInstanceForms[typeof(T)];
            formToActivate.BringToFront();
            //if (formToActivate.WindowState != FormWindowState.Maximized)
            //{
            //    formToActivate.WindowState = FormWindowState.Maximized;
            //}
            
            //formToActivate.Show();
            //formToActivate.Activate();

            return formToActivate;
        }
    }
}